﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AddSiteColumnToList.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>
//   Creates or Updates a Content Type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Collaboris.Saf.Actions.Wss.Column
{
    using System.Collections.Generic;

    using Entities;

    using Engine;

    using SharePoint.Wss.Attributes;

    using Utils;
    using SharePoint.Wss.Lists;

    [CollaborisClass(
      Title = "Add a Site Column to a SharePoint List.",
      ReleaseStatus = ReleaseStatus.Beta,
      DisableDocumentation = false,
      ContactAuthorEmail = "Support@Collaboris.co.uk",
      Summary = "Adds a site column to an existing SharePoint list.",
      UndoImplemented = false,
      ClassGuid = "{156E17A5-A7BA-4e1f-8A48-1BC341A7C727}")]
    public class AddSiteColumnToList : WSSActionBase<AddSiteColumnToListInfo>
    {
        #region Helper Methods

        /// <summary>
        /// Executes the Action using the values passed in by the Data Entity
        /// and the <see cref="SafContext"/>.
        /// </summary>
        /// <param name="context">
        /// A valid <see cref="SafContext"/> containing properties to run the Action with.
        /// </param>
        /// <param name="entity">
        /// A Valid <see cref="AddSiteColumnToListInfo"/> containing the parameters with which to run the Action with.
        /// </param>
        /// <returns>
        /// A <see cref="ProcessStatus"/> signifying the outcome of processing the Action.
        /// </returns>
        protected override ProcessStatus DoAction(SafContext context, AddSiteColumnToListInfo entity)
        {
            this.Trace.TraceMethodStart("DoAction");
            ValidationHelper.VerifyObjectArgument(context.Site, "context.Site");

            this.Trace.TraceVerbose("Adding this SiteColumn {0} to this list {1}", entity.FieldName, entity.ListName);

            string fieldSiteCollection ;
            if (string.IsNullOrEmpty(entity.FieldCollectionWebUrl))
                fieldSiteCollection = TargetWeb.Url;
            else
            {
                fieldSiteCollection = entity.FieldCollectionWebUrl;
            }

            SPListHelper.AddFieldToList(TargetWeb, entity.ListName, fieldSiteCollection, entity.FieldName);

            this.Trace.TraceMethodEnd("DoAction");
            return ProcessStatus.Success;
        }

        /// <summary>
        /// Checks the Data entity used by the Action for Validity
        /// </summary>
        /// <param name="context">
        /// The <see cref="SafContext"/> passed in by the SAF Engine.
        /// </param>
        /// <param name="entity">
        /// The data entity to Validate.
        /// </param>
        /// <param name="errors">
        /// List of <see cref="ValidationError"/>s
        /// </param>
        /// <param name="mode">
        /// <see cref="ProcessMode"/>signalling wheter we are Doing, or Undoing the action.
        /// </param>
        /// <returns>
        /// false if invalid; otherwise true.
        /// </returns>
        protected override bool IsValid(
            SafContext context, AddSiteColumnToListInfo entity, ref List<ValidationError> errors, ProcessMode mode)
        {
            int currentCount = errors.Count;

            // Validate each object now.
            ValidateStringForNull(entity.ListName, "AddSiteColumnToListInfo.ListName", ref errors);
            ValidateStringForNull(entity.FieldName, "AddSiteColumnToListInfo.FieldName", ref errors);

            // Have we added errors ?
            return errors.Count == currentCount;
        }

      #endregion
    }
}